<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>骡窝窝系统管理平台</title>
    <#include "../common/header.ftl"/>
    <script type="text/javascript" src="/js/plugins/My97DatePicker/WdatePicker.js"></script>

    <script type="text/javascript">
        $(function(){
            $("#pagination").twbsPagination({
                totalPages:${pageInfo.pages}||1,
                    visiblePages:5,
                    startPage:${qo.currentPage},
            first:"首页",
                    prev:"上一页",
                    next:"下一页",
                    last:"尾页",
                    onPageClick:function(event,page){
                $("#currentPage").val(page);
                $("#searchForm").submit();
            }
        });

            //分页
            $("#query").click(function(){
                $("#currentPage").val(1);
                $("#searchForm").submit();
            });


            //图片显示
            //选择器 取出来的是一堆 数据
            var imgs = $('.imgUrls');
            //遍历每一个元素
            for (n=0;n < imgs.length;n++){
                //拿到每个元素
                var temp = $(imgs[n]);
                //每个标签里的内容
                //进行切割
                var contents =  temp.html().split(';')
                //清空标签里原来的字符串
                temp.empty();
                //在原来的标签下进行 追加图片的子元素，因为有多张图片的情况所以要 遍历追加
                for(i=0;i<contents.length -1;i++){
                    //追加 图片
                    temp.append("<img src="+contents[i]+" width='50px' style='margin-right:5px' >");
                }
            }

            //点击修改按钮
            $(".inputBtn").click(function () {
                // 4步走,清空,取值,回显,显示
                // 1. 清空
                $('#inputModal input').val('');
                $('#inputModal select').val('');
                $('#inputModal img').attr('src','');
                // 2. 取值
                var obj = $(this).data('json');
                if (obj){
                    $.get('/product/getContent.do',{id:obj.id},function (data) {
                        //商品详情
                        $('#content').html(data.content);
                    });
                }
                // 3. 编辑回显
                if (obj) {
                    $('#inputModal input[name="id"]').val(obj.id);
                    $('#inputModal input[name="productName"]').val(obj.productName);
                    $('#inputModal input[name="introduce"]').val(obj.introduce);
                    $('#inputModal input[name="price"]').val(obj.price);
                    $('#inputModal select[name="status"]').val(obj.status);
                    $('#coverUrl').attr('src',obj.coverUrl);
                    //进行切割
                    console.log(obj.id);
                    var contents =  obj.pics.split(';');
                    $('#pic1').append("<img src="+contents[0]+" width='50px' style='margin-right:5px' >")
                    $('#pic2').append("<img src="+contents[1]+" width='50px' style='margin-right:5px' >")
                    $('#pic3').append("<img src="+contents[2]+" width='50px' style='margin-right:5px' >")
                }
                // 4. 显示
                $('#inputModal').modal('show');
            });


            //定义全局变量,传递图片框绑定的标识
            var did;
            //图片框点击事件
            $(".rect").click(function () {
                $("[name='upload']").click();
                did = $(this).data("did");
            })


            //图片改变事件,提交表单
            $("[name='upload']").change(function () {
                var url;
                $("#imgForm").ajaxSubmit(function (data) {
                    url = data.url;
                    //上传后的图片回显
                    $('.rect[data-did="' + did + '"]').html("<img name='uploadimgs' width='80px' src='"+url+"'></img>");
                })
            });

            //评论提交按钮点击事件,提交表单
            $("#saveBtn").click(function () {
                //获取更多评价内容
                var content = $("[name='content']").val();
                var id = $('#inputModal input[name="id"]').val();
                var productName = $('#inputModal input[name="productName"]').val();
                var introduce = $('#inputModal input[name="introduce"]').val();
                var price = $('#inputModal input[name="price"]').val();
                var status = $('#inputModal select[name="status"]').val();


                //获取上传图片
                var images = '';
                var coverUrl= '';
                $("[name='uploadimgs']").each(function (index, ele) {
                    var url = $(ele).attr("src");
                    if (coverUrl.length==0){
                        coverUrl = url;
                    } else {
                        //避免第一个开始时拼接分号
                        if (images.length == 0) {
                            images += url;
                        } else {
                            images = images + ";" + url;
                        }
                    }
                });
                //为末尾拼接;
                images = images + ";";

                if (id){
                    var data = {
                        id: id,
                        productName: productName,
                        introduce: introduce,
                        price: price,
                        status: status,
                        'content.content': content,//商品详细
                        coverUrl:coverUrl,//主图
                        pics: images//图片
                    }
                } else {
                    var data = {
                        productName: productName,
                        introduce: introduce,
                        price: price,
                        status: status,
                        'content.content': content,//商品详细
                        coverUrl:coverUrl,//主图
                        pics: images//图片
                    }
                }

                //data是页面所有要提交的数据
                $.post("/product/saveOrUpdate.do", data, function (data) {
                    if (data.success) {
                        window.parent.location.reload();
                    }
                });
            });
        });
    </script>
    <style type="text/css">
        .modal-body{
            height:500px;
            overflow:scroll;
        }
    </style>
</head>

<body>

<div class="container">
<#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
        <#assign currentMenu = "product" />
				<#include "../common/menu.ftl" />
        </div>
        <div class="col-sm-9">
            <div class="page-header">
                <h3>商品管理</h3>
            </div>
            <div class="row">
                <!-- 提交分页的表单 -->
                <form id="searchForm" class="form-inline" method="post" action="/product/list.do">
                    <input type="hidden" name="currentPage" id="currentPage" value="1"/>
                    <div class="form-group">
                    </div>
                    <div class="form-group">
                        <label>商品分类</label>
                        <select class="form-control" id="status" name="status" autocomplete="off">
                            <option value="-1">全部</option>
                            <option value="1">食品</option>
                            <option value="2">生活用品</option>
                            <option value="3">电子产品</option>
                            <option value="4">衣服</option>
                        </select>
                        <script type="application/javascript">$('#status').val(${qo.status})</script>
                    </div>
                    <div class="form-group">
                        <button id="query" type="button" class="btn btn-success"><i class="icon-search"></i> 查询</button>
                        <a href="javascript:" class="btn btn-success inputBtn">添加商品</a>
                    </div>
                </form>
            </div>
            <div class="row">
                <table class="table">
                    <thead>
                    <tr>
                        <th>名称</th>
                        <th>主图</th>
                        <th>图片</th>
                        <th>简单介绍</th>
                        <th>价格</th>
                        <th>分类</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
					<#list pageInfo.list as vo>
                    <tr>
                        <td>${vo.productName}</td>
                        <td><img src='${vo.coverUrl!""}' width="50px"/></td>
                        <td class="imgUrls">${vo.pics!""} </td>
                        <td>${vo.introduce}</td>
                        <td>${vo.price}</td>
                        <td>
                            <#if vo.status == 1>
                                食品
                            <#elseif vo.status == 2>
                                生活用品
                            <#elseif vo.status == 3>
                                电子产品
                            <#elseif vo.status == 4>
                                衣服
                            </#if>
                        </td>
                        <td>
                            <a href="javascript:" class="btn btn-sm btn-success inputBtn" data-json='${vo.json}'>
                                <span class="glyphicon glyphicon-edit " ></span> 修改
                            </a>
                        </td>
                    </tr>
					</#list>
                    </tbody>
                </table>

                <div style="text-align: center;">
                    <ul id="pagination" class="pagination"></ul>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">编辑/增加</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" name="id">
                <div class="form-group">
                    <label class="col-sm-3 control-label">名称</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" id="productName" name="productName" placeholder="名称">
                    </div>
                </div>
                <form id="imgForm" class="form-horizontal" method="post" action="/images.do" enctype="multipart/form-data">
                    <input type="file" style="display: none" name="upload"/>
                </form>
                <div class="form-group">
                    <label class="col-sm-3 control-label">主图</label>
                    <div class="col-sm-7 rect" data-did="1">
                        <span>上传</span>
                        <img id="coverUrl" width="80px"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">图片</label>
                    <div class="col-sm-7 rect" data-did="2" id="pic1">
                        <span>上传</span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">图片</label>
                    <div class="col-sm-7 rect" data-did="3" id="pic2">
                        <span>上传</span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">图片</label>
                    <div class="col-sm-7 rect" data-did="4" id="pic3">
                        <span>上传</span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">简单介绍</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" id="introduce" name="introduce" placeholder="简单介绍">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">商品详细</label>
                    <div class="col-sm-7">
                        <textarea rows="10" cols="10" class="form-control" id="content" name="content"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">价格</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" id="price" name="price" placeholder="价格">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">分类</label>
                    <div class="col-sm-7">
                        <select id="status" class="form-control" autocomplete="off" name="status" >
                            <option value="1">食品</option>
                            <option value="2">生活用品</option>
                            <option value="3">电子产品</option>
                            <option value="4">衣服</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <a href="javascript:void(0);" class="btn btn-success" id="saveBtn" aria-hidden="true">保存</a>
                <a href="javascript:void(0);" class="btn" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>

</body>
</html>